// index.js
const app = getApp(), core = require('../../utils/core'), util = require('../../utils/util');
Component({

  options: {
    styleIsolation: 'apply-shared'
  },

  /**
   * 组件的属性列表
   */
  properties: {
    //用于定位picker index
    "current_city_id": {type: Number, value: 0},
  },

  /**
   * 组件的初始数据
   */
  data: {
    //picker's range property
    cities: [],
    index: [],
  },

  lifetimes: {
    attached: function() {
      util.getSiteCities().then(cities => {
        let index = 0;
        for(let i in cities) {
          if(cities[i].city_id==this.data.current_city_id) {
            index = i;
            break;
          }
        }
        this.setData({cities, index});
      });
    },
    detached: function() {
    },
  },

  /**
   * 组件的方法列表
   */
  methods: {
    _bindchange: function(event) {
      let selectedCity = this.data.cities[event.detail.value];
      let city = {id: selectedCity.city_id, name: selectedCity.city_name};
      app.setCurrentCity(city);
    }
  },
  observers: {
    'current_city_id': function(value) {
      let index = 0;
      for(let i in this.data.cities) {
        if(this.data.cities[i].city_id==value) {
          index = i;
          break;
        }
      }
      this.setData({index});
    },
  }
});
